package com.example.springboot_ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_ruiji.common.R;
import com.example.springboot_ruiji.dao.EmployeeDao;
import com.example.springboot_ruiji.exception.NameExistsException;
import com.example.springboot_ruiji.pojo.Employee;
import com.example.springboot_ruiji.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.Optional;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public R<Employee> login(Employee employee) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one=employeeDao.selectOne(queryWrapper);
        if (one==null){
         return R.error("用户不存在");
        }
        if (!DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()).equals(one.getPassword())){
            return R.error("密码不正确");
        }
        if (one.getStatus()!=1){
            return R.error("账户被禁用");
        }
        return R.success(one);
    }

    @Override
    public R page(Integer page, Integer pageSize, String name) {
        QueryWrapper<Employee> queryWrapper= new QueryWrapper<>();
        queryWrapper.like(name!=null,"name",name);
        Page<Employee> employeePage = new Page<>(page, pageSize);
        Page<Employee> employeePage1 = employeeDao.selectPage(employeePage, queryWrapper);
        if (employeePage1.getSize()==0){
            return R.error("查询失败,无数据");
        }else {
            return R.success(employeePage1);
        }
    }

    @Override
    public R add(Employee employee) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee employee1 = employeeDao.selectOne(queryWrapper);
        if (employee1!=null){
            throw  new NameExistsException(employee.getUsername());
        }
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        employee.setCreateUser(1L);
        employee.setUpdateUser(1L);
        int i = employeeDao.insert(employee);
        if (i!=0) {
            return R.success("成功");
        }else {
            return R.error("失败");
        }
    }

    @Override
    public R toEdit(Long id) {
        if (Optional.of(id).isPresent()){
            Employee employee = employeeDao.selectById(id);
            if (employee==null){
                return R.error("未查询到员工信息");
            }
            return R.success(employee);
        }
        return R.error("参数不能为空");
    }

    @Override
    public R edit(Employee employee) {
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        employee.setCreateUser(1L);
        employee.setUpdateUser(1L);
        int i = employeeDao.updateById(employee);
        if (i!=0) {
            return R.success("成功");
        }else {
            return R.error("失败");
        }
    }
}
